package com.langchain4j.simple.config;

import com.langchain4j.simple.service.NumberExtractor;
import com.langchain4j.simple.service.PersonExtractor;
import com.langchain4j.simple.service.SentimentAnalyzer;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName LangChainConfig
 * @createTime 10:43:00 2025/04/21
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class LangChainConfig {

    @Bean
    public ChatModel chatLanguageModel() {
        log.info("apiKey: {}", System.getenv("DASHSCOPE_KEY"));
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwen-turbo")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)//开启请求日志
                .logResponses(true)//开启响应日志
                .maxRetries(1) //设置重试策略
                .build();
    }


    @Bean
    public SentimentAnalyzer sentimentAnalyzer(ChatModel chatLanguageModel) {
        return AiServices.create(SentimentAnalyzer.class, chatLanguageModel);
    }

    @Bean
    public NumberExtractor numberExtractor(ChatModel chatLanguageModel) {
        return AiServices.create(NumberExtractor.class, chatLanguageModel);
    }

    @Bean
    public PersonExtractor personExtractor(ChatModel chatLanguageModel) {
        return AiServices.create(PersonExtractor.class, chatLanguageModel);
    }
}
